import fs from "fs";
import path from "path";
import unzipper from "unzipper"
import { saveAs } from 'file-saver'
import {
  getNowMilliSecond,
  getcurDate,
  getAppLocalPath,
  getSystemDir,
  checkDiffList,
  checkDiffListYJZG, getZGTypeResv, getComTypeResv
} from "@/utils/common"
import iconv from "iconv-lite";
import {fn} from "sequelize"
import compressing from "compressing";
import Minizip from "minizip-asm.js";
import {getFeedback, getFileSize, getJLID, setNodeOfUpAndExport} from "@/utils/JsonTools";
import {decodeFile, decodeFile2, ecodeFile} from "@/utils/CodingsUtils";
import moment from "moment";
// import iconv from "iconv-lite";
const password='lanjian@chrtc'
// 获前存储路径
// export const  getSystemDir=()=> {
//   // 获取保存路径
//   // eslint-disable-next-line node/handle-callback-err
//   //path = fs.readFileSync('dbson/Filepath.dbson', 'utf-8')
//   let  pathlocal= path.resolve('./chrtc')
//   let platform = process.platform;
//   if (platform === 'win32') {
//     // 处理Windows平台的路径
//     pathlocal=path.resolve('./chrtc')
//   } else if (platform === 'linux') {
//     // 处理Linux平台的路径
//     pathlocal=path.join(__dirname,"..").replace("resources","")+"chrtc"
//     if(!fs.existsSync(pathlocal)){
//       fs.mkdirSync(pathlocal)
//     }
//   } else {
//     // 处理其他平台的路径
//   }
//   return pathlocal
// }
//根据路径，创建文件
export const   mkdir=(dirname)=>{
  if (fs.existsSync(dirname)) {
    console.log('创建失败1')
    return true
  } else {
    console.log('创建失败2')
    if (mkdir(path.dirname(dirname))) {
      console.log('创建成功3')
      fs.mkdirSync(dirname)
      return true
    }
  }
}


//加密文件带密码
function zipFile(){
  const fs = require('fs');
  const crypto = require('crypto');
  const archiver = require('archiver');

// 创建目标压缩包流
  const output = fs.createWriteStream('demo.zip');
// 设置配置选项
  const archive = archiver('zip', {
    zlib: { level: 9 } // 设置压缩级别
  });
// 监听关闭
  output.on('close', function() {
    console.log('压缩完成');
  });


  const cipher = crypto.createCipher('aes-256-cbc', password);

// 添加待压缩的目录
  archive.directory('target/', false);

// 加密压缩流
  archive.pipe(cipher).pipe(output);

// 执行压缩
  archive.finalize();

}
//解密zip文件带密码
export const unzipFile=(product)=> {
  return new Promise(async (resolve, reject) => {
    try {
      const path = require('path')
      const extractPath = getSystemDir() + "/" + getNowMilliSecond() + "/"
      const importFilename = product.name
      const importFilePath = product.path
      let fileExt = product.name.slice(product.name.lastIndexOf('.') + 1)
      const newzipPath = getSystemDir() + "/" + "import_" + getNowMilliSecond() + ".zip"
      console.log('fff-1--', extractPath)
      //mkdir(extractPath)
      const iconv = require('iconv-lite')
      let xfname = ''
      console.log("fp====", importFilePath)
      unzip(importFilePath, extractPath)
      fs.copyFileSync(importFilePath, newzipPath)
      const resb = fileReads(extractPath + "/" + importFilename.replace(".zip", ""), newzipPath, importFilename)
      resolve(resb)
      //   unzipper.Open.file(importFilePath).then((d) => {
      //     for (let i = 0; i < d.files.length; i++) {
      //       const file = d.files[i]
      //       const tmpFilePath = extractPath + "/" + iconv.decode(file.pathBuffer, 'utf8')
      //       const fname = iconv.decode(d.files[i].pathBuffer, 'utf8')
      //       console.log('dis---', file.type + ":" + iconv.decode(d.files[i].pathBuffer, 'utf8'))
      //       if (i === 0) {
      //         xfname = fname;
      //         console.log('fnnn-1--', xfname)
      //         mkdir(tmpFilePath)
      //       } else {
      //         if (fname == xfname + '/data' || fname == xfname + '/file') {
      //           mkdir(tmpFilePath)
      //         } else {
      //            d.files[i].stream(password).pipe
      //            (fs.createWriteStream(tmpFilePath))
      //             .on('finish', () => {
      //               resolve(tmpFilePath);
      //               console.log("文件读取结束---",tmpFilePath)
      //             })
      //             .on('error', reject);
      //         }
      //       }
      //     }
      //    /// resolve(2)
      //   }).then((rec) => {
      //    console.log('file(s) written to ',rec)
      //      // setTimeout((b)=>{
      //      //   fileReads(extractPath+"/"+xfname,newzipPath,importFilename)
      //      //   resolve(1)
      //      //  },5000)
      // }).catch((e) => {
      //    // fileReads(extractPath+"/"+xfname,newzipPath,importFilename)
      //   //resolve(0)
      // });
    }catch (e) {
      resolve(0)
    }
  })
}
////////
function unzip(zipFile, destFolder){
  console.log("文件---",zipFile)
  const AdmZip = require('adm-zip');
  let zip = new AdmZip(zipFile);
  zip.extractAllTo(destFolder,true,true,password)
  //  var zipEntries = zip.getEntries();
  //  for(var i=0; i<zipEntries.length; i++)
  //  {
  //    var entry = zipEntries[i];
  //    entry.entryName = iconv.decode(entry.rawEntryName, 'gbk');
  //  }
  //  zip.extractAllTo(destFolder,false,true,password);
}
//文件解析导入
function fileReads(parhf,newZippath,zipname){
  //判断是否未一个单位，1 同一个,2，不同个  不一个单位清空
  const fkjson_null={
    data:[]
  }
  let fksjtime=""
  const readMe_parh=parhf+"/Readme.json";
  const readMe_details_json=fs.existsSync(parhf+"/data/lanjian_supervision_feedback_details")?decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_feedback_details","utf-8")):JSON.stringify(fkjson_null)
  console.log('pp---',readMe_details_json)
  const readMe_json=decodeFile(fs.readFileSync(readMe_parh,"utf-8"))
  console.log("rm---",readMe_json)
  if(fs.existsSync(parhf+"/data/lanjian_supervision_feedback_details")){
    let fksjtime=getcurDate()
    if(fs.existsSync(parhf+"/data/lanjian_supervision_feedback")){
      const bss=decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_feedback","utf-8"))
      console.log('pp-112--',bss)
      fksjtime=JSON.parse(JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_feedback","utf-8"))).data)[0].F_FKSJ_
    }
    console.log("fr--",fksjtime)
    //如果单位不一致
    // if((JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)!=(JSON.parse(readMe_dbson).sessionId+JSON.parse(readMe_dbson).roundId+JSON.parse(readMe_dbson).unitId)){
    //   return 3;
    // }
    //组装反馈意见信息
    let fkyj_dbson_list=JSON.parse('[]')
    const fkyj_json_list=JSON.parse(readMe_details_json)
    const jls_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianJLSelected.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianJLSelected.dbson","utf-8"))
    const unilist=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    //创建一个主表id
    const zgfa_main_id=getNowMilliSecond()+""
    //创建一个主表id
    const zgtz_main_id=getNowMilliSecond()+""
    //设置默认选中结论组
    if(jls_dbson==''){
      const sj=JSON.parse(readMe_json)

      jls_dbson.push(sj)
      fs.writeFileSync(getAppLocalPath()+"dbson/lanjianJLSelected.dbson",JSON.stringify(jls_dbson))
    }else{
      const unilist2=unilist.filter(function (value){
        return value.jlid==JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
      })
      if(unilist2==undefined&&unilist2.length==0){
        const sj=JSON.parse(readMe_json)

        jls_dbson.push(sj)
        fs.writeFileSync(getAppLocalPath()+"dbson/lanjianJLSelected.dbson",JSON.stringify(jls_dbson))
      }

    }
    /*---------------------------处理预警配置-----------------------------------*/
    if(fs.existsSync(parhf+"/data/lanjian_supervision_warning")){
      const waring_dbson=decodeFile(fs.readFileSync(getAppLocalPath()+"dbson/Warings.dbson","utf-8"))
      let waring_json=""
      if(fs.existsSync(parhf+"/data/lanjian_supervision_warning")){
        console.log("ww--",JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_warning","utf-8"))))
        waring_json=fs.readFileSync(parhf+"/data/lanjian_supervision_warning","utf-8")==""?"":JSON.parse(JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_warning","utf-8"))).data)
      }
      let waring_list=[]
      if(waring_dbson!=""){
        waring_list=JSON.parse(waring_dbson)
      }
      if(waring_list.length===0){
        waring_json[0].fksj=fksjtime
        waring_json[0].jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
        waring_list.push(waring_json[0])
        console.log("war--",waring_list.length)
        fs.writeFileSync(getAppLocalPath()+"dbson/Warings.dbson",JSON.stringify(waring_list))
      }else{
        const listw=waring_list.filter(function (value) {
          return value.jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
        })
        if(listw==undefined||listw.length==0){
          waring_json[0].fksj=fksjtime
          waring_json[0].jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
          waring_list.push(waring_json[0])
          fs.writeFileSync(getAppLocalPath()+"dbson/Warings.dbson",JSON.stringify(waring_list))
        }
      }

    }
    /*---------------------------处理反馈意见-----------------------------------*/
    const unitlist=fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")
    if(unitlist==""){
      const unit_list=[]
      const sj=JSON.parse(readMe_json)
      sj.fksj=fksjtime
      unit_list.push(sj)
      fs.writeFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson",JSON.stringify(unit_list))
    }else{
      const unit_list=JSON.parse(unitlist)
      let lsarr = unit_list.filter(function(value){
        //筛选条件
        return value.sessionId+""+value.roundId+""+value.unitId==JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
      })
      if(lsarr==undefined||lsarr.length==0){
        const sj=JSON.parse(readMe_json)
        sj.fksj=fksjtime
        unit_list.push(sj)
        fs.writeFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson",JSON.stringify(unit_list))
      }
    }
    fkyj_dbson_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8"))
    let lsarr_fkyj = fkyj_dbson_list.filter(function(value){
      //筛选条件
      return value.jlid==JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
    })
    console.log('de---',lsarr_fkyj)
    console.log("fk----",fkyj_json_list)
    //没有新增
    if(lsarr_fkyj===undefined||lsarr_fkyj.length===0){
      JSON.parse(fkyj_json_list.data).forEach(item=>{
        const it=JSON.parse('{}')
        it.id=item.ID_
        it.remake=item.F_BZ_
        it.Problems=item.F_WTFL_
        it.concreteProblems=item.F_WTFM_
        it.ProblemClassification=item.F_JTWT_
        it.serial_num= item.SERIAL_NUM_
        it.jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
        //整改方案字段
        it.zgfa_id=zgfa_main_id
        it.zgtz_id=zgtz_main_id
        it.zgfa_measure=""
        it.zgfa_time=""
        it.zgfa_department=""
        it.zgfa_leader=""
        it.zgfa_persion=""
        it.zgfa_zgtype=""
        it.zgfa_endtime=""
        it.zgfa_mark=""
        it.zgfa_pzz_infos=""
        it.zgfa_jdjcs_infos=""
        it.zgfa_pzz_yj=""
        it.zgfa_jdjcs_yj=""
        it.plan_status=2
        it.update_time=moment(new Date()).format("YYYY-MM-DD")
        fkyj_dbson_list.push(it)
      })
      fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson",JSON.stringify(fkyj_dbson_list))
    }
    else{//有根据id排除现有的数据
      console.log("size-4--",fkyj_dbson_list)
      console.log("size-5--",fkyj_json_list.data)
      const {toAdd, toRemove, mergedList} = checkDiffList(fkyj_dbson_list,JSON.parse(fkyj_json_list.data))
      console.log("size-3---",mergedList)
      //更新条目
      mergedList.forEach(itm=>{
        if(itm.id===undefined){
          itm.id=itm.ID_
          itm.remake=itm.F_BZ_
          itm.Problems=itm.F_WTFL_
          itm.concreteProblems=itm.F_WTFM_
          itm.ProblemClassification=itm.F_JTWT_
          itm.serial_num= itm.SERIAL_NUM_
          itm.jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
          //整改方案字段
          itm.zgfa_id=mergedList[0].zgfa_main_id
          itm.zgtz_id=mergedList[0].zgtz_main_id
          itm.zgfa_measure=""
          itm.zgfa_time=""
          itm.zgfa_department=""
          itm.zgfa_leader=""
          itm.zgfa_persion=""
          itm.zgfa_zgtype=""
          itm.zgfa_endtime=""
          itm.zgfa_mark=""
          itm.zgfa_pzz_infos=""
          itm.zgfa_jdjcs_infos=""
          itm.zgfa_pzz_yj=""
          itm.zgfa_jdjcs_yj=""
          itm.plan_status=2
          itm.update_time=moment(new Date()).format("YYYY-MM-DD")
        }
      })
      //更新数据
      const newBackList=JSON.parse(fkyj_json_list.data)
      mergedList.forEach(itm=>{
        newBackList.forEach(itmn=>{
          if( itm.id==itmn.ID_){
            itm.remake=itmn.F_BZ_
            itm.Problems=itmn.F_WTFL_
            itm.concreteProblems=itmn.F_WTFM_
            itm.ProblemClassification=itmn.F_JTWT_
            itm.serial_num= itmn.SERIAL_NUM_
          }

        })
      })
      fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson",JSON.stringify(mergedList))
    }
    console.log('readme---',readMe_json)
    importAndOutRecord("反馈意见",newZippath,zipname,"导入")
  }
  // /*---------------------------处理导入模板-----------------------------------*/
  if(fs.existsSync(parhf+"/data/lanjian_supervision_template")){
    const all_list=[]
    const tmp_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Templete.dbson")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Templete.dbson"))
    //const ttt= fs.readFileSync(parhf+"/data/lanjian_supervision_template")
    //console.log("tttt-",decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_template")))
    const tmp_dbson2=JSON.parse(JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_template"))).data)
    if(tmp_dbson.length==0){
      //处理文件
      for(var i=0;i<tmp_dbson2.length;i++){
        tmp_dbson2[i].jlid=getJLID()
        try{
          const mx=JSON.parse(tmp_dbson2[i].F_MBXX_)
          tmp_dbson2[i].fname=mx[0].name
          const paths = parhf+"/file/"+mx[0].newFname
          console.log("pf--",paths)
          if(fs.existsSync(paths)){
            console.log("pf123--",paths)
            const newpath = getSystemDir()+"/"+mx[0].newFname
            fs.copyFileSync(paths,newpath)
            tmp_dbson2[i].paths=newpath
          }
        }catch (e) {
          console.log("pftt--",e)
        }
        all_list.push(tmp_dbson2[i])
      }
      fs.writeFileSync("dbson/Templete.dbson",JSON.stringify(all_list))
      //all_list.push(tmp_dbson2)
    }else{
      const tmp_dbson3=tmp_dbson.filter(function (value){
        return value.jlid=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
      })
      if(tmp_dbson3.length==0||tmp_dbson3==undefined){
        for(var i=0;i<tmp_dbson2.length;i++){
          tmp_dbson2[i].jlid=getJLID()
          try{
            const mx=JSON.parse(tmp_dbson2[i].F_MBXX_)
            tmp_dbson2[i].fname=mx[0].name
            const paths = parhf+"/file/"+mx[0].newFname
            console.log("pf--",paths)
            if(fs.existsSync(paths)){
              console.log("pf3--",paths)
              const newpath = getSystemDir()+"/"+mx[0].newFname
              fs.copyFileSync(paths,newpath)
              tmp_dbson2[i].paths=newpath
            }
            all_list.push(tmp_dbson2[0])
          }catch (e) {
            console.log("pftt2--",e)
          }

        }
        fs.writeFileSync("dbson/Templete.dbson",JSON.stringify(all_list))
      }else{
        fs.writeFileSync("dbson/Templete.dbson",JSON.stringify(tmp_dbson))
      }

    }

  }
  /*---------------------------导入研究整改工作-----------------------------------*/
  if(fs.existsSync(parhf+"/data/lanjian_supervision_work")&&fs.existsSync(parhf+"/data/lanjian_supervision_approve_history")){
    const units_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    const imp_jl=units_list.filter(it=>{
      return (it.sessionId+it.roundId+it.unitId)===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    if(imp_jl.length===0){
      return 0
    }
    const history=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const his_list=JSON.parse(history.data);
    const his_last=[];
    his_list.forEach(itemh=>{
      itemh.jlid=JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId
      //处理文件
      const filelist=(itemh.F_SMCL_==""||itemh.F_SMCL_==undefined)?[]:JSON.parse(itemh.F_SMCL_)
      let listfile=[]
      //处理附件问题
      if(filelist.length>0){
        filelist.forEach(item=>{
          const fname = item.fileName
          const oldpath=parhf+"/file/"+item.newFname
          const copypath= getSystemDir()+"/"+item.newFname
          if(fs.existsSync(oldpath)){
            fs.copyFileSync(oldpath,copypath)
          }
          const file={
            tfname:fname,
            tfpath:copypath
          }
          listfile.push(file)
        })
      }
      itemh.gkfj = listfile
      his_last.push(itemh)
    })
    //history.data=his_last
    console.log("mme---",history)
    const meetHis_list=fs.readFileSync(getAppLocalPath()+"dbson/MEETINGHistory.dbson","utf-8")==""?[]:JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/MEETINGHistory.dbson","utf-8")).data
    const meet_filt_list=meetHis_list.filter(item=>{
      return item.jlid===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    console.log("mme2---",meet_filt_list)
     const { toAdd, toRemove, mergedList }=checkDiffListYJZG(meet_filt_list,his_list)
      history.data=meet_filt_list.concat(toAdd)
    console.log("mme3---",history.data)
      fs.writeFileSync(getAppLocalPath()+"dbson/MEETINGHistory.dbson",JSON.stringify(history))
    // }
    const work_json_local=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/RectificationMeeting.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationMeeting.dbson","utf-8"))
   if(work_json_local.length==0){
      return 0
    }
    const work_json=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_work","utf-8")))

    const chek_work= []

    const work_json_data=JSON.parse(work_json.data)
    console.log("wk--",work_json_data)
    const history_json=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const history_json_data=JSON.parse(history_json.data)
    work_json_local.forEach(item=>{
      work_json_data.forEach(hitem=>{
        if(item.id==hitem.ID_){
          item.f_status=hitem.F_STATUS_
          chek_work.push(item)
        }
      })
    })
    if(chek_work.length==0){
      return 0
    }
    //更新研究整改工作数据
    fs.writeFileSync(getAppLocalPath()+"dbson/RectificationMeeting.dbson",JSON.stringify(work_json_local))
    //更新导入导出记录
    importAndOutRecord("研究整改工作",newZippath,zipname,"导入")
  }

  /*---------------------------导入整改方案-----------------------------------*/

  const list_plan_main=[]
  const list_plan_list=[]

  if(fs.existsSync(parhf+"/data/lanjian_supervision_plan")&&fs.existsSync(parhf+"/data/lanjian_supervision_plan_details")&&fs.existsSync(parhf+"/data/lanjian_supervision_approve_history")) {



    const units_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    const imp_jl=units_list.filter(it=>{
      return (it.sessionId+it.roundId+it.unitId)===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    if(imp_jl.length===0){
      return 0
    }

    const fanan_json=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_plan","utf-8")))
    const plan_total=JSON.parse(fanan_json.data)

    console.log("p_t---",plan_total)

    const history=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const his_list=JSON.parse(history.data);
    const his_last=[];
    his_list.forEach(itemh=>{
      itemh.jlid=getJLID()
      his_last.push(itemh)
      //处理文件
      const filelist=(itemh.F_SMCL_==""||itemh.F_SMCL_==undefined)?[]:JSON.parse(itemh.F_SMCL_)
      for(var i=0;i<filelist.length;i++){
        console.log("pp--",parhf+"/file/"+filelist[i].newFname)
        if(fs.existsSync(parhf+"/file/"+filelist[i].newFname)){
          fs.copyFileSync(parhf+"/file/"+filelist[i].newFname,getSystemDir()+"/"+filelist[i].newFname)
          filelist[i].path=getSystemDir()+"/"+filelist[i].newFname
        }else{
          filelist[i].path=""
        }
      }
    })
    history.data=his_last
    fs.writeFileSync(getAppLocalPath()+"dbson/ZGFAHistory.dbson",JSON.stringify(history))
    const fanan_dbson=fs.readFileSync(getAppLocalPath()+"dbson/RectificationPlanAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationPlanAllinfos.dbson","utf-8")
    const plan_total_db=JSON.parse(fanan_dbson)
    //处理主表数据
    plan_total_db.map(item=>{
      plan_total.map(hitem=>{
        if((item.id+"")==hitem.ID_){
          const spqk_infos=JSON.parse(hitem.F_PGQK_==""?"[]":hitem.F_PGQK_)
          console.log("size3--",hitem)
          spqk_infos.forEach(itsp=>{
            if(itsp.roleKey=="pzz"){
              console.log("size5--",spqk_infos)
              item.planTotal.pzz_pg_infos=itsp.illustrate
              item.planTotal.pzz_editor=itsp.editor
              item.planTotal.pzz_phone=itsp.telephone
            }
            if(itsp.roleKey=="jdjcs"){
              item.planTotal.jdjcs_pg_infos=itsp.illustrate
              item.planTotal.jdjcs_editor=itsp.editor
              item.planTotal.jdjcs_phone=itsp.telephone
            }
          })
          let listfile=[]
          //处理附件问题
          if(hitem.F_FJ_!=""){
            const fj_list=JSON.parse(hitem.F_FJ_)
            fj_list.forEach(item=>{
              const fname = item.fileName
              const oldpath=parhf+"/file/"+item.newFname
              const copypath= getSystemDir()+"/"+item.newFname
              if(fs.existsSync(oldpath)){
                fs.copyFileSync(oldpath,copypath)
              }
              const file={
                tfname:fname,
                tfpath:copypath
              }
              listfile.push(file)
            })
          }
          item.planTotal.fafj = listfile
          item.f_status=hitem.F_STATUS_
          item.planTotal.ldyj= hitem.F_LDYJ_==undefined?"":hitem.F_LDYJ_
        }
      })
      // item.fksj=hitem.F_FKSJ_
      list_plan_main.push(item)
    })
    console.log("size6--",list_plan_main)
    fs.writeFileSync(getAppLocalPath()+"dbson/RectificationPlanAllinfos.dbson",JSON.stringify(list_plan_main))
    //处理子表数据
    const fanan_json_list=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_plan_details","utf-8")))
    const plan_total_list=JSON.parse(fanan_json_list.data)

    const fanan_dbson_list=fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8")
    const plan_total_db_list=JSON.parse(fanan_dbson_list)
    //处理主表数据
    plan_total_db_list.forEach(item=>{
      plan_total_list.forEach(hitem=>{
        if((item.id+"")==hitem.F_FKYJID_){
          const spqk_infos=JSON.parse(hitem.F_PGQK_==""?"[]":hitem.F_PGQK_)
          spqk_infos.forEach(itsp=>{
            if(itsp.roleKey=="pzz"){
              item.zgfa_pzz_infos=itsp.illustrate
              item.zgfa_pzz_yj=itsp.opinion===undefined?"":itsp.opinion
            }else{
              // item.zgfa_pzz_infos=""
              // item.zgfa_pzz_yj=""
            }
            if(itsp.roleKey=="jdjcs"){
              item.zgfa_jdjcs_infos=itsp.illustrate
              item.zgfa_jdjcs_yj=itsp.opinion===undefined?"":itsp.opinion
            }else{
              // item.zgfa_jdjcs_infos=""
              // item.zgfa_jdjcs_yj=itsp.opinion=""
            }
          })
          console.log("size123--",spqk_infos)
        }
      })
      list_plan_list.push(item)
    })
    console.log("size7--",list_plan_list)
    fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson",JSON.stringify(list_plan_list))
    importAndOutRecord("整改方案",newZippath,zipname,"导入")
  }

  /*----------------------------------------------整改台账导入----------------------------------------------*/

  if(fs.existsSync(parhf+"/data/lanjian_supervision_ledger_details")&&fs.existsSync(parhf+"/data/lanjian_supervision_approve_history")&&fs.existsSync(parhf+"/data/lanjian_supervision_ledger")){


    const units_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    const imp_jl=units_list.filter(it=>{
      return (it.sessionId+it.roundId+it.unitId)===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    if(imp_jl.length===0){
      return 0
    }

    const history=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const his_list=JSON.parse(history.data);
    const his_last=[];
    his_list.forEach(itemh=>{
      itemh.jlid=getJLID()
      his_last.push(itemh)
      //处理文件
      const filelist=(itemh.F_SMCL_==""||itemh.F_SMCL_==undefined)?[]:JSON.parse(itemh.F_SMCL_)
      for(var i=0;i<filelist.length;i++){
        console.log("pp--",parhf+"/file/"+filelist[i].newFname)
        if(fs.existsSync(parhf+"/file/"+filelist[i].newFname)){
          fs.copyFileSync(parhf+"/file/"+filelist[i].newFname,getSystemDir()+"/"+filelist[i].newFname)
          filelist[i].path=getSystemDir()+"/"+filelist[i].newFname
        }else{
          filelist[i].path=""
        }
      }
    })
    history.data=his_last
    fs.writeFileSync(getAppLocalPath()+"dbson/LeaderHistory.dbson",JSON.stringify(history))

    const tzmain_json_list=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_ledger","utf-8")))
    const tzmain_total_list=JSON.parse(tzmain_json_list.data)
    const gkmain=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Rectification.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Rectification.dbson","utf-8"))
    if(gkmain.length===0){
      tzmain_total_list[0].jlid=getJLID()
      gkmain.push(tzmain_total_list[0])
      fs.writeFileSync(getAppLocalPath()+"dbson/Rectification.dbson",JSON.stringify(gkmain))
    }else{
      console.log("tz--",tzmain_total_list)
      const lis=gkmain.filter(function (value) {
        return  value.jlid===JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
      })
      console.log("tz-2-",lis)
      if(lis.length==0){
        tzmain_total_list[0].jlid=getJLID()
        gkmain.push(tzmain_total_list[0])
        fs.writeFileSync(getAppLocalPath()+"dbson/Rectification.dbson",JSON.stringify(gkmain))
      }else{
        const lis_back=gkmain.filter(function (value) {
          return  value.jlid!=JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId
        })
        lis[0].F_STATUS_=tzmain_total_list[0].F_STATUS_
        lis_back.push(lis[0])
        fs.writeFileSync(getAppLocalPath()+"dbson/Rectification.dbson",JSON.stringify(lis_back))
      }
    }
    const tz_json_list=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_ledger_details","utf-8")))
    const tz_total_list=JSON.parse(tz_json_list.data)

    const tz_dbson_list=fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson","utf-8")
    const tz_total_db_list=JSON.parse(tz_dbson_list)
    //处理主表数据
    tz_total_db_list.forEach(item=>{
      tz_total_list.forEach(hitem=>{
        if((item.id+"")==hitem.F_FKYJID_){
          item.tz_ldyj=hitem.F_LDYJ_
          item.tz_f_fj=hitem.F_FJ_
          const spqk_infos=JSON.parse(hitem.F_PGQK_==""?"[]":hitem.F_PGQK_)
          spqk_infos.forEach(itsp=>{
            if(itsp.roleKey=="pzz"){
              item.tz_pzz_infos=itsp.illustrate
              item.tz_pzz_yj=itsp.opinion
              item.tz_pzz_phone=itsp.telephone
              item.tz_pzz_editor=itsp.editor
            }else{
              // item.tz_pzz_infos=""
              // item.tz_pzz_yj=""
              // item.tz_pzz_phone=""
              // item.tz_pzz_editor=""
            }
            if(itsp.roleKey=="jdjcs"){
              item.tz_jdjcs_infos=itsp.illustrate
              item.tz_jdjcs_yj=itsp.opinion
              item.tz_jdjcs_phone=itsp.telephone
              item.tz_jdjcs_ditor=itsp.editor
            }else{
              // item.tz_jdjcs_infos=""
              // item.tz_jdjcs_yj=""
              // item.tz_jdjcs_phone=""
              // item.tz_jdjcs_ditor=""
            }
            if(itsp.roleKey=="dfs"){
              item.tz_dfs_infos=itsp.illustrate
              item.tz_dfs_yj=itsp.opinion
              item.tz_dfs_phone=itsp.telephone
              item.tz_dfs_editor=itsp.editor
            }else{
              // item.tz_dfs_infos=""
              // item.tz_dfs_yj=""
              // item.tz_dfs_phone=""
              // item.tz_dfs_editor=""
            }
          })
          let listfile=[]
          //处理附件问题
          if(hitem.F_FJ_!=""){
            const fj_list=JSON.parse(hitem.F_FJ_==undefined?"[]":hitem.F_FJ_)
            fj_list.forEach(item=>{
              const fname = item.fileName
              const oldpath=parhf+"/file/"+item.newFname
              const copypath= getSystemDir()+"/"+item.newFname
              if(fs.existsSync(oldpath)){
                fs.copyFileSync(oldpath,copypath)
              }
              const file={
                tfname:fname,
                tfpath:copypath
              }
              listfile.push(file)
            })
          }
          item.tzfj = listfile
        }
      })
      list_plan_list.push(item)
    })
    console.log("size7--",list_plan_list)
    fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson",JSON.stringify(list_plan_list))
    importAndOutRecord("整改台账",newZippath,zipname,"导入")
  }
  /*----------------------------------------------整改报告导入----------------------------------------------*/
  if(fs.existsSync(parhf+"/data/lanjian_supervision_report")&&fs.existsSync(parhf+"/data/lanjian_supervision_approve_history")){

    const rep_local_list=[]

    const units_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    const imp_jl=units_list.filter(it=>{
      return (it.sessionId+it.roundId+it.unitId)===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    if(imp_jl.length===0){
      return 0
    }

    const history=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const his_list=JSON.parse(history.data);
    const his_last=[];
    his_list.forEach(itemh=>{
      itemh.jlid=getJLID()
      his_last.push(itemh)
      //处理文件
      const filelist=(itemh.F_SMCL_==""||itemh.F_SMCL_==undefined)?[]:JSON.parse(itemh.F_SMCL_)
      for(var i=0;i<filelist.length;i++){
        console.log("pp--",parhf+"/file/"+filelist[i].newFname)
        if(fs.existsSync(parhf+"/file/"+filelist[i].newFname)){
          fs.copyFileSync(parhf+"/file/"+filelist[i].newFname,getSystemDir()+"/"+filelist[i].newFname)
          filelist[i].path=getSystemDir()+"/"+filelist[i].newFname
        }else{
          filelist[i].path=""
        }
      }
    })
    history.data=his_last
    fs.writeFileSync(getAppLocalPath()+"dbson/ReportHistory.dbson",JSON.stringify(history))

    const rep_json_list=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_report","utf-8")))
    const rep_total_list=JSON.parse(rep_json_list.data)

    const rep_dbson_list=fs.readFileSync(getAppLocalPath()+"dbson/RectificationReportAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationReportAllinfos.dbson","utf-8")
    const rep_total_db_list=JSON.parse(rep_dbson_list)
    //处理主表数据
    rep_total_db_list.forEach(item=>{
      rep_total_list.forEach(hitem=>{
        if(item.jlid==JSON.parse(readMe_json).sessionId+""+JSON.parse(readMe_json).roundId+""+JSON.parse(readMe_json).unitId){
          const spqk_infos=JSON.parse(hitem.F_PGQK_==""?"[]":hitem.F_PGQK_)
          item.f_status=hitem.F_STATUS_
          item.planTotal.ldyj=hitem.F_LDYJ_
          spqk_infos.forEach(itsp=>{
            if(itsp.roleKey=="xcz"){
              item.planTotal.report_xcz_pg_infos=itsp.illustrate
              item.planTotal.report_xcz_pg_yj=itsp.opinion
              item.planTotal.report_xcz_editor=itsp.editor
              item.planTotal.report_xcz_phone=itsp.telephone
            }
            if(itsp.roleKey=="swzzb"){
              item.planTotal.report_swzzb_pg_infos=itsp.illustrate
              item.planTotal.report_swzzb_pg_yj=itsp.opinion
              item.planTotal.report_swzzb_editor=itsp.editor
              item.planTotal.report_swzzb_phone=itsp.telephone
            }
            if(itsp.roleKey=="pzz"){
              item.planTotal.report_pzz_pg_infos=itsp.illustrate
              item.planTotal.report_pzz_pg_yj=itsp.opinion
              item.planTotal.report_pzz_editor=itsp.editor
              item.planTotal.report_pzz_phone=itsp.telephone
            }
            if(itsp.roleKey=="jdjcs"){
              item.planTotal.report_jdjcs_pg_infos=itsp.illustrate
              item.planTotal.report_jdjcs_pg_yj=itsp.opinion
              item.planTotal.report_jdjcs_editor=itsp.editor
              item.planTotal.report_jdjcs_phone=itsp.telephone
            }
            if(itsp.roleKey=="dfs"){
              item.planTotal.report_dfs_pg_infos=itsp.illustrate
              item.planTotal.report_dfs_pg_yj=itsp.opinion
              item.planTotal.report_dfs_editor=itsp.editor
              item.planTotal.report_dfs_phone=itsp.telephone
            }
          })
          let listfile=[]
          //处理附件问题
          if(hitem.F_FJ_!=""){
            const fj_list=JSON.parse(hitem.F_FJ_)
            fj_list.forEach(item=>{
              const fname = item.fileName
              const oldpath=parhf+"/file/"+item.newFname
              const copypath= getSystemDir()+"/"+item.newFname
              if(fs.existsSync(oldpath)){
                fs.copyFileSync(oldpath,copypath)
              }
              const file={
                tfname:fname,
                tfpath:copypath
              }
              listfile.push(file)
            })
          }
          item.planTotal.bgfj = listfile
        }
      })
      rep_local_list.push(item)
    })
    console.log("size8--",rep_local_list)
    fs.writeFileSync(getAppLocalPath()+"dbson/RectificationReportAllinfos.dbson",JSON.stringify(rep_local_list))
    importAndOutRecord("整改报告",newZippath,zipname,"导入")
  }
  /*----------------------------------------------整改公开导入----------------------------------------------*/
  if(fs.existsSync(parhf+"/data/lanjian_supervision_public")&&fs.existsSync(parhf+"/data/lanjian_supervision_approve_history")){

    const rep_local_list=[]

    const history=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_approve_history","utf-8")))
    const his_list=JSON.parse(history.data);
    const his_last=[];

    const units_list=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/lanjianUnitInfos.dbson","utf-8"))
    const imp_jl=units_list.filter(it=>{
      return (it.sessionId+it.roundId+it.unitId)===(JSON.parse(readMe_json).sessionId+JSON.parse(readMe_json).roundId+JSON.parse(readMe_json).unitId)
    })
    if(imp_jl.length===0){
      return 0
    }
    his_list.forEach(itemh=>{
      itemh.jlid=getJLID()
      //处理文件
      const filelist=(itemh.F_SMCL_==""||itemh.F_SMCL_==undefined)?[]:JSON.parse(itemh.F_SMCL_)
      let listfile=[]
      //处理附件问题
      if(filelist.length>0){
        filelist.forEach(item=>{
          const fname = item.fileName
          const oldpath=parhf+"/file/"+item.newFname
          const copypath= getSystemDir()+"/"+item.newFname
          if(fs.existsSync(oldpath)){
            fs.copyFileSync(oldpath,copypath)
          }
          const file={
            tfname:fname,
            tfpath:copypath
          }
          listfile.push(file)
        })
      }
      itemh.gkfj = listfile
      his_last.push(itemh)
    })
    history.data=his_last
    fs.writeFileSync(getAppLocalPath()+"dbson/ZGGKHistory.dbson",JSON.stringify(history))

    const rep_json_list=JSON.parse(decodeFile(fs.readFileSync(parhf+"/data/lanjian_supervision_public","utf-8")))
    const rep_total_list=JSON.parse(rep_json_list.data)

    const rep_dbson_list=fs.readFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson","utf-8")
    const rep_total_db_list=JSON.parse(rep_dbson_list)
    //处理主表数据
    rep_total_db_list.forEach(item=>{
      rep_total_list.forEach(hitem=>{
        if((item.id+"")==hitem.ID_){
          item.planTotal.f_status=hitem.F_STATUS_
        }
      })
      rep_local_list.push(item)
    })
    console.log("size8--",rep_local_list)
    fs.writeFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson",JSON.stringify(rep_local_list))
    importAndOutRecord("整改公开",newZippath,zipname,"导入")
  }
  return 1;
}
//文件数据流
export function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    let buffers = []
    stream.on('error', reject)
    stream.on('data', (data) => buffers.push(data))
    const m= stream.on('end', () => resolve(Buffer.concat(buffers)))
    console.log("mm--",m)
  })
}
//导出提报 1，研究整改  2 整改方案 3 整改台账 4 整改报告 5 整改公开 0 全部
export function exportModule(type,zippath) {
  return new Promise((resolve, reject) => {
    const unitSelected=getJLSelected();
    if(unitSelected.length==0){
      resolve(0)
      return
    }
    const iconv = require('iconv-lite')
    const folder=unitSelected[0].fileDesc
    const Minizip = require('minizip-asm.js');
    const fs = require("fs");
    const text = new Buffer("{'psd':'密码1234567'}");
    const mz = new Minizip();
    //创建文件夹
    mkdir(zippath+"/")
    mkdir(zippath+"/file")
    mkdir(zippath+"/data")
    let module_name=unitSelected[0].unitName+"_"+unitSelected[0].sessionName+"_"+unitSelected[0].roundName
    let tenantId=unitSelected[0].tenantId
    const sys_files= {
      data:[],
      "tableName": "sys_file"
    }
    const lanjian_supervision_work= {
      data:[],
      "tableName": "lanjian_supervision_work"
    }
    const lanjian_supervision_plan= {
      data:[],
      "tableName": "lanjian_supervision_plan"
    }
    const lanjian_supervision_plan_details= {
      data:[],
      "tableName": "lanjian_supervision_plan_details"
    }
    const lanjian_supervision_ledger= {
      data:[],
      "tableName": "lanjian_supervision_ledger"
    }
    const lanjian_supervision_ledger_details= {
      data:[],
      "tableName": "lanjian_supervision_ledger_details"
    }
    const lanjian_supervision_report= {
      data:[],
      "tableName": "lanjian_supervision_report"
    }
    const lanjian_supervision_public= {
      data:[],
      "tableName": "lanjian_supervision_public"
    }
    /*---------------------------------------研究整改工作------------------------------------------------------*/
    if(type==1||type==0){
      const list_back=[]
      const meeting_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/RectificationMeeting.dbson","utf-8"))
      if(meeting_dbson.length==0){
        resolve(0)
        return;
      }
      const jl_dbson=fs.readFileSync(getAppLocalPath()+"dbson/lanjianJLSelected.dbson","utf-8")
      if(jl_dbson==""){
        resolve(0)
        return;
      }
      meeting_dbson.forEach(item=>{
        const filelist=[]
        //console.log('dd--',JSON.parse(jl_dbson)[0].unitId)
        const work={}
        const f_status=item.f_status!=undefined?item.f_status:"0"

        if(f_status!="2"&&f_status!=5) {
          console.log("sss--", f_status)
          work.F_XCZDX_REF_ID_ = JSON.parse(jl_dbson)[0].unitId
          work.ID_ = item.id
          work.CREATE_DEP_ID_ = "1"
          work.TENANT_ID_ = tenantId+""
          work.F_HYZT_ = item.theme
          work.F_HYLX_ = item.meetingType
          work.F_HYRQ_ = item.dateOfMeeting
          work.F_HYKSSJ_ = item.startMeetingTime
          work.F_HYJSSJ_ = item.endMeetingTime
          work.F_ZCR_ = item.host
          work.F_CHRY_ = item.participants
          work.F_HYZY_ = item.meetingSummary
          work.F_VERSION_ = item.version+""
          work.F_STATUS_ = item.f_status != undefined ? ((item.f_status == "3" || item.f_status == "6") ? "4" : item.f_status) : "1"
          work.CREATE_BY_ = "1"
          work.CREATE_TIME_ = item.uploadTime
          console.log("sss--", work.F_STATUS_)
          if (item.fileList != undefined && item.fileList.length > 0) {
            let f_list = []
            item.fileList.forEach(its => {
              const fid = its.zgfid
              const sysfile = {}
              const file = {}
              file.uid = fid + ""
              file.name = its.name
              file.fileId = fid + ""
              file.fileName = its.name
              file.size = its.filesize
              let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
              file.newFname = `${fid}.${fileExt}`
              file.path = "operation/" + file.newFname
              filelist.push(file)
              //复制文件
              fs.copyFileSync(its.url, zippath + '/file/' + file.newFname)
              //组装sys_file 文件shuju
              // sysfile.TOTAL_BYTES_=its.filesize
              // sysfile.FILE_ID_=fid
              // sysfile.NEW_FNAME_=file.newFname
              // sysfile.UPDATE_TIME_=its.updateTime
              // sysfile.CREATE_TIME_=its.updateTime
              // sysfile.EXT_=fileExt
              // sysfile.DEL_STATUS_="undeleted"
              // sysfile.PATH_=file.path
              // sysfile.FILE_NAME_=its.name
              // sysfile.CREATE_BY_="1"
              // sysfile.TENANT_ID_="1"
              // sysfile.FILE_SYSTEM_="file"
              // sysfile.ENCRYPT="0"
              //f_list.push(sysfile)
            })
            work.F_HYCL_ = filelist
          }
          if (f_status === "0" || f_status === undefined) {
            item.f_status = "1"
            //lanjian_supervision_work.data.push(work)
          }
          if (f_status == "3" || f_status == "6") {
            item.f_status = "4"
            //lanjian_supervision_work.data.push(work)
          }
          lanjian_supervision_work.data.push(work)

        }
        list_back.push(item)
      })
      if(lanjian_supervision_work.data.length==0){
        resolve(2)
        return
      }
      fs.writeFileSync(getAppLocalPath()+'dbson/RectificationMeeting.dbson',JSON.stringify(list_back))
      console.log("00--",lanjian_supervision_work)
      fs.writeFileSync(zippath+'/data/lanjian_supervision_work',ecodeFile(JSON.stringify(lanjian_supervision_work)))

      // //mz.append(encodeGbk(folder+"/data/sys_file"), JSON.stringify(sys_files), {password: password});
      // mz.append(encodeGbk(folder+"/data/lanjian_supervision_work"), JSON.stringify(lanjian_supervision_work), {password: password});
      // mz.append(encodeGbk(folder+"/Readme.json"), JSON.stringify(unitSelected[0]), {password: password});

      unitSelected[0].module="研究整改工作"
      fs.writeFileSync(zippath+"/Readme.json",ecodeFile(JSON.stringify(unitSelected[0])))
      console.log("rmm---",unitSelected[0])
      module_name=module_name+"_"+"研究整改工作_"+getNowMilliSecond()


      compressing.zip.compressDir(zippath, zippath+ '.zip').then(() => {
        let fileb = fs.createReadStream(zippath+ '.zip')
        streamToBuffer(fileb).then(res => {
          let data = res
          let contents = new Uint8Array(data.length)
          for (let i = 0; i < data.length; i++) {
            contents[i] = data[i]
          }
          encrypt(new Blob([contents]),password,resolve,'导出',module_name,"研究整改工作")
        })
      })

    }
    /*---------------------------------------整改方案------------------------------------------------------*/
    if(type==2||type==0){
      const filelist=[]
      const plantotal_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/RectificationPlanAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationPlanAllinfos.dbson","utf-8"))
      if(plantotal_dbson.length==0){
        resolve(0)
        return;
      }
      if(plantotal_dbson[0].planTotal===undefined||plantotal_dbson[0].planTotal.editor===undefined){
        resolve(2)
        return;
      }
      console.log("1313---",plantotal_dbson)
      const jl_dbson=getJLSelected()
      if(jl_dbson.length==0){
        resolve(0)
        return;
      }
      let lsarr = plantotal_dbson.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        resolve(0)
        return;
      }
      if(lsarr[0].f_status=="2"||lsarr[0].f_status=="5"){
        resolve(2)
        return;
      }
      let mainid="";
      lsarr.forEach(item=>{
        //console.log('dd--',JSON.parse(jl_dbson)[0].unitId)
        mainid=item.id+""
        const plan={}
        plan.F_XCZDX_REF_ID_=jl_dbson[0].unitId
        plan.ID_=item.id+""
        plan.CREATE_DEP_ID_="1"
        plan.TENANT_ID_=tenantId+""
        plan.F_TJR_=item.planTotal.editor
        plan.F_LXDH_=item.planTotal.phone
        plan.F_VERSION_=item.version+""
        plan.F_STATUS_=item.f_status==undefined?"1":(item.f_status=="3"||item.f_status=="6")?"4":"1"
        if(item.planTotal.f_status!="2"&&item.planTotal.f_status!="5"){
          plan.F_LDYJ_=""
          plan.F_PGQK_=""
          plan.F_FJ_=""
        }
        plan.CREATE_BY_="1"
        plan.CREATE_TIME_=getcurDate()
        if(item.planTotal.fileList.length>0){
          item.planTotal.fileList.forEach(its=>{
            const fid=getNowMilliSecond()
            const sysfile={}
            const file={}
            file.uid=fid+""
            file.name=its.name
            file.fileId=fid+""
            file.fileName=its.name
            file.size=its.filesize
            let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
            file.newFname=`${fid}.${fileExt}`
            file.path="operation/"+file.newFname
            filelist.push(file)
            //复制文件
            fs.copyFileSync(its.url, zippath+'/file/'+file.newFname)
            //组装sys_file 文件shuju
            // sysfile.TOTAL_BYTES_=its.filesize
            // sysfile.FILE_ID_=fid
            // sysfile.NEW_FNAME_=file.newFname
            // sysfile.UPDATE_TIME_=its.updateTime
            // sysfile.CREATE_TIME_=its.updateTime
            // sysfile.EXT_=fileExt
            // sysfile.DEL_STATUS_="undeleted"
            // sysfile.PATH_=file.path
            // sysfile.FILE_NAME_=its.name
            // sysfile.CREATE_BY_="1"
            // sysfile.TENANT_ID_="1"
            // sysfile.FILE_SYSTEM_="file"
            // sysfile.ENCRYPT="0"
            // sys_files.data.push(sysfile)
          })
          plan.F_ZGFA_=filelist

          lanjian_supervision_plan.data.push(plan)
        }
      })
      console.log("fafaf-1--",lanjian_supervision_plan)
      const back_list=[]
      //子表数据
      const plan_details=  JSON.parse(fs.readFileSync(getAppLocalPath()+'dbson/Feedback.dbson', 'utf-8'))
      // const list_back_feeedback=[]
      const plan_details_list = plan_details.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })

      const edit_list=plan_details_list.filter(function(value){

        //筛选条件
        return (value.zgfa_measure==undefined||value.zgfa_measure=="")
      })
      console.log("55--",edit_list)
      if(edit_list.length>0){
        resolve(2)
        return;
      }
      // if(plan_details_list.length< plan_details.length){
      //   resolve(2)
      //   return;
      // }
      // plan_details.forEach(value=>{
      //   if(value.zgfa_pzz_yj==="2"){
      //     value.zgfa_pzz_yj="3"
      //   }
      //   if(value.zgfa_jdjcs_yj==="2"){
      //     value.zgfa_jdjcs_yj="3"
      //   }
      //   value.plan_status=1
      //   back_list.push(value)
      // })
      // console.log("sisi---",back_list)
      const feedbak_list=[]
      plan_details_list.forEach(item=>{
        const plan_d={}
        plan_d.F_XCZDX_REF_ID_=jl_dbson[0].unitId
        plan_d.ID_=item.id+""
        plan_d.CREATE_DEP_ID_="1"
        plan_d.TENANT_ID_=tenantId+""
        plan_d.F_ZGCS_=item.zgfa_measure
        plan_d.F_ZRBM_=item.zgfa_department
        plan_d.F_ZRLD_=item.zgfa_leader
        plan_d.F_ZRR_=item.zgfa_persion
        if(item.zgfa_endtime==undefined||item.zgfa_endtime==""){

        } else{
          plan_d.F_WCSX_=item.zgfa_endtime
        }
        plan_d.F_ZGLX_=item.zgfa_zgtype
        plan_d.F_FKYJID_=item.id+""
        plan_d.F_PLANID_=mainid
        plan_d.CREATE_BY_="1"
        plan_d.CREATE_TIME_=getcurDate()
        plan_d.F_PGQK_=[]

        if(item.zgfa_pzz_yj==="1"){
          const pzzDo={
            roleKey:"pzz",
            opinion:item.zgfa_pzz_yj,
            telephone:"",
            editor:"",
            illustrate:item.zgfa_pzz_infos
          }
          plan_d.F_PGQK_.push(pzzDo)
        }

        if(item.zgfa_jdjcs_yj==='1'){
          const jdjcsDo={
            roleKey:"jdjcs",
            opinion:item.zgfa_jdjcs_yj,
            telephone:"",
            editor:"",
            illustrate:item.zgfa_jdjcs_infos
          }
          plan_d.F_PGQK_.push(jdjcsDo)
        }

        if(item.zgfa_jdjcs_yj==="2"||item.zgfa_pzz_yj==="2"){
          item.zgfa_pzz_yj=0+""
          item.zgfa_jdjcs_yj=0+""
          plan_d.F_PGQK_=[]
        }

        if(plan_d.F_PGQK_.length===0){
          plan_d.F_PGQK_=""
        }

        if(item.zgfa_pzz_yj!="1"){
          item.zgfa_pzz_yj=0+""
        }
        if(item.zgfa_jdjcs_yj!="1") {
          item.zgfa_jdjcs_yj=0+""
        }

        // if(item.zgfa_pzz_yj==='2'||item.zgfa_jdjcs_yj==='2'){
        //   item.zgfa_pzz_yj=0+""
        //   item.zgfa_jdjcs_yj=0+""
        // }
        //


        lanjian_supervision_plan_details.data.push(plan_d)
      })

      console.log("fafafdddd11---",lanjian_supervision_plan)
      console.log("fafafdddd---",lanjian_supervision_plan_details)
      console.log("fafafdddd22---",plan_details_list)

      plan_details.forEach(ib=>{
        plan_details_list.forEach(ibs=>{
          if(ib.id===ibs.id){
            ib.zgfa_pzz_yj=ibs.zgfa_pzz_yj
            ib.zgfa_jdjcs_yj=ibs.zgfa_jdjcs_yj
          }
        })
      })

      fs.writeFileSync(getAppLocalPath()+'dbson/Feedback.dbson',JSON.stringify(plan_details))
      fs.writeFileSync(zippath+'/data/lanjian_supervision_plan',ecodeFile(JSON.stringify(lanjian_supervision_plan)))
      fs.writeFileSync(zippath+'/data/lanjian_supervision_plan_details',ecodeFile(JSON.stringify(lanjian_supervision_plan_details)))

      unitSelected[0].module="整改方案"
      fs.writeFileSync(zippath+"/Readme.json",ecodeFile(JSON.stringify(unitSelected[0])))
      module_name=module_name+"_"+"整改方案_"+getNowMilliSecond()

      // plantotal_dbson[0].f_status=lsarr[0].f_status==undefined?"1":"4"
      // fs.writeFileSync(`dbson/RectificationPlanAllinfos.dbson`,JSON.stringify(plantotal_dbson))
      // fs.writeFileSync("dbson/Feedback.dbson",JSON.stringify(back_list))

      compressing.zip.compressDir(zippath, zippath+ '.zip').then(() => {
        let fileb = fs.createReadStream(zippath+ '.zip')
        streamToBuffer(fileb).then(res => {
          let data = res
          let contents = new Uint8Array(data.length)
          for (let i = 0; i < data.length; i++) {
            contents[i] = data[i]
          }
          encrypt(new Blob([contents]),password,resolve,'导出',module_name,"整改方案")
        })
      })

    }
    /*---------------------------------------整改台账------------------------------------------------------*/
    if(type==3||type==0){
      const tz_dbson=  JSON.parse(fs.readFileSync(getAppLocalPath()+'dbson/Feedback.dbson', 'utf-8'))
      const jl_dbson=getJLSelected()
      if(jl_dbson.length==0){
        resolve(0)
        return;
      }
      let lsarr = tz_dbson.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        resolve(0)
        return;
      }
      // const planmain=JSON.parse(fs.readFileSync("dbson/RectificationPlanAllinfos.dbson","utf-8"))
      // if(planmain.length==0){
      //   resolve(0)
      //   return;
      // }
      // let lsarrmain = planmain.filter(function(value){
      //   //筛选条件
      //   return value.jlid==getJLID()
      // })
      // if(lsarrmain==undefined||lsarrmain.length==0){
      //   resolve(0)
      //   return;
      // }
      // const mid=lsarrmain[0].id+""


      let tzid=getNowMilliSecond()

      const main_status=fs.readFileSync(getAppLocalPath()+"dbson/Rectification.dbson","utf-8")==""?undefined:JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Rectification.dbson","utf-8"))


      if(main_status==undefined)
      {

      }else{
        if(main_status.length>0&&(main_status[0].F_STATUS_=="2"||main_status[0].F_STATUS_=="5"))
        {
          resolve(2)
          return;
        }
      }


      let lsarrinfos = lsarr.filter(function(value){
        //筛选条件
        return (value.zgtz_zrr==undefined||value.zgtz_whss==undefined||value.zgtz_wszd==undefined||value.zgtz_complete_info==undefined)
      })
      if(lsarrinfos.length>0){
        resolve(2)
        return;
      }
      // mkdir(zippath+"/file")
      // console.log("mk--",zippath+"/file")
      fs.writeFileSync(zippath+"/file/readme.json","")
      // fs.unlinkSync(zippath+"/file/readme.json")
      lsarr.forEach(item=>{
        const tz_d={}
        tzid=item.zgtz_id
        tz_d.F_ZGTZID_=item.zgtz_id
        tz_d.F_ZGFAID_=item.id+""
        tz_d.F_XCZDX_REF_ID_=jl_dbson[0].unitId
        tz_d.ID_=item.id+""
        tz_d.CREATE_DEP_ID_="1"
        tz_d.TENANT_ID_=tenantId+""
        tz_d.F_FKYJID_=item.id+""
        tz_d.F_ZZWZ_=item.zgtz_zrr
        tz_d.F_WHSS_=item.zgtz_whss
        tz_d.F_WSZD_=item.zgtz_wszd
        tz_d.F_WCQK_=item.zgtz_complete_info+""
        tz_d.F_BZ_=item.zgtz_marks
        tz_d.CREATE_BY_="1"
        tz_d.CREATE_TIME_=getcurDate()
        tz_d.F_PGQK_=[]
        if(item.tz_pzz_yj=="1"){
          const pzzDo={
            roleKey:"pzz",
            opinion:item.tz_pzz_yj,
            telephone:item.tz_pzz_phone,
            editor:item.tz_pzz_editor,
            illustrate:item.tz_pzz_infos
          }
          tz_d.F_PGQK_.push(pzzDo)
          tz_d.F_LDYJ_=item.tz_ldyj
          tz_d.F_FJ_=item.tz_f_fj
        }
        if(item.tz_jdjcs_yj=="1"){
          const jdjcsDo={
            roleKey:"jdjcs",
            opinion:item.tz_jdjcs_yj,
            telephone:item.tz_jdjcs_phone,
            editor:item.tz_jdjcs_ditor,
            illustrate:item.tz_jdjcs_infos
          }
          tz_d.F_PGQK_.push(jdjcsDo)
          tz_d.F_LDYJ_=item.tz_ldyj
          tz_d.F_FJ_=item.tz_f_fj
        }

        if(item.tz_dfs_yj=="1"){
          const dfsDo={
            roleKey:"dfs",
            opinion:item.tz_dfs_yj,
            telephone:item.tz_dfs_phone,
            editor:item.tz_dfs_editor,
            illustrate:item.tz_dfs_infos
          }
          tz_d.F_PGQK_.push(dfsDo)
          tz_d.F_LDYJ_=item.tz_ldyj
          tz_d.F_FJ_=item.tz_f_fj
        }

        if(item.tz_pzz_yj==="2"||item.tz_jdjcs_yj==="2"||item.tz_dfs_yj==="2"){
          item.tz_pzz_yj="0"
          item.tz_jdjcs_yj="0"
          item.tz_dfs_yj="0"
          tz_d.F_PGQK_=[]
        }
        if(item.tz_pzz_yj!='1'){
          item.tz_pzz_yj="0"
        }
        if(item.tz_jdjcs_yj!='1'){
          item.tz_jdjcs_yj="0"
        }
        if(item.tz_dfs_yj!='1'){
          item.tz_dfs_yj="0"
        }
        if(tz_d.F_PGQK_.length===0){
          tz_d.F_PGQK_=""
          tz_d.F_FJ_=""
        }
        lanjian_supervision_ledger_details.data.push(tz_d)
      })
      tz_dbson.forEach(ib=>{
        lsarr.forEach(ibs=>{
          if(ib.id===ibs.id){
            ib.tz_pzz_yj=ibs.tz_pzz_yj
            ib.tz_jdjcs_yj=ibs.tz_jdjcs_yj
            ib.tz_dfs_yj=ibs.tz_dfs_yj
          }
        })
      })

      fs.writeFileSync(getAppLocalPath()+'dbson/Feedback.dbson',JSON.stringify(tz_dbson))
      const sm = ecodeFile(JSON.stringify(lanjian_supervision_ledger_details))
      console.log("ld--2-",lanjian_supervision_ledger_details)
      fs.writeFileSync(zippath+'/data/lanjian_supervision_ledger_details',sm)

      let lsarr_tz = main_status===undefined?[]:main_status.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      const main_leader={}
      main_leader.F_XCZDX_REF_ID_=jl_dbson[0].unitId
      main_leader.ID_=tzid
      main_leader.CREATE_DEP_ID_="1"
      main_leader.TENANT_ID_=tenantId+""
      main_leader.F_VERSION_=lsarr_tz.length===0?getNowMilliSecond()+"":lsarr_tz[0].version+""
      main_leader.CREATE_BY_="1"
      main_leader.CREATE_TIME_=getcurDate()
      if(main_status==undefined||main_status.length===0){
        main_leader.F_STATUS_="1"
      }else{
        main_leader.F_STATUS_=main_status[0].F_STATUS_==undefined?"1":(main_status[0].F_STATUS_=="3"||main_status[0].F_STATUS_=="6")?"4":"2"
      }

      lanjian_supervision_ledger.data.push(main_leader)

      const sm2 = ecodeFile(JSON.stringify(lanjian_supervision_ledger))
      console.log("ld--21-",sm)
      console.log("ld--221-",decodeFile2(sm2))
      console.log("ld--2-",sm)
      console.log("ld--22-",decodeFile2(sm))
      fs.writeFileSync(zippath+'/data/lanjian_supervision_ledger',sm2)

      unitSelected[0].module="整改台账"
      fs.writeFileSync(zippath+"/Readme.json",ecodeFile(JSON.stringify(unitSelected[0])))
      module_name=module_name+"_"+"整改台账_"+getNowMilliSecond()

      compressing.zip.compressDir(zippath, zippath+ '.zip').then(() => {
        let fileb = fs.createReadStream(zippath+ '.zip')
        streamToBuffer(fileb).then(res => {
          let data = res
          let contents = new Uint8Array(data.length)
          for (let i = 0; i < data.length; i++) {
            contents[i] = data[i]
          }
          encrypt(new Blob([contents]),password,resolve,'导出',module_name,"整改台账")
        })
      })

    }
    /*---------------------------------------整改报告------------------------------------------------------*/
    if(type==4||type==0){
      const filelist=[]
      const report_dbson=fs.readFileSync(getAppLocalPath()+"dbson/RectificationReportAllinfos.dbson","utf-8")
      if(report_dbson==""){
        resolve(0)
        return;
      }
      const jl_dbson=getJLSelected()
      if(jl_dbson.length==0){
        resolve(0)
        return;
      }
      let lsarr = JSON.parse(report_dbson).filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        resolve(0)
        return;
      }
      if(lsarr[0].f_status=="2"||lsarr[0].f_status=="5"){
        resolve(2)
        return;
      }

      console.log("item--",lsarr)
      lsarr.forEach(item=>{

        //console.log('dd--',JSON.parse(jl_dbson)[0].unitId)
        const repprt={}
        repprt.F_XCZDX_REF_ID_=jl_dbson[0].unitId
        repprt.ID_=item.planTotal.id+""
        repprt.CREATE_DEP_ID_="1"
        repprt.TENANT_ID_=tenantId+""
        repprt.F_TJR_=item.planTotal.editor
        repprt.F_LXDH_=item.planTotal.phone
        repprt.F_VERSION_=item.planTotal.version+""
        repprt.F_STATUS_=item.f_status==undefined?"1":(item.f_status=="3"||item.f_status=="6")?"4":"1"
        repprt.CREATE_BY_="1"
        repprt.CREATE_TIME_=getcurDate()
        if(item.planTotal.fileList.length>0){
          item.planTotal.fileList.forEach(its=>{
            const fid=getNowMilliSecond()
            const sysfile={}
            const file={}
            file.uid=fid+""
            file.name=its.name
            file.fileId=fid+""
            file.fileName=its.name
            file.size=its.filesize
            let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
            file.newFname=`${fid}.${fileExt}`
            file.path="operation/"+file.newFname
            filelist.push(file)
            //复制文件
            fs.copyFileSync(its.url, zippath+'/file/'+file.newFname)
            //   const pzzDo={
            //     roleKey:"xcz",
            //     opinion:item.report_xcz_pz_yj,
            //     // telephone:"",
            //     // editor:"",
            //     // illustrate:""
            //   }
            //   const jdjcsDo={
            //     roleKey:"jdjcs",
            //     opinion:item.report_jdjcs_pz_yj,
            //     // telephone:"",
            //     // editor:"",
            //     // illustrate:""
            //   }
            //   const swzzb={
            //     roleKey:"swzzb",
            //     opinion:item.ruleForm.report_swzzb_pz_yj,
            //     // telephone:"",
            //     // editor:"",
            //     // illustrate:""
            //   }
            //   const dfsDo={
            //     roleKey:"pzz",
            //     opinion:item.zgtz_pzz_yj,
            //     // telephone:"",
            //     // editor:"",
            //     // illustrate:""
            //   }
            //   repprt.F_PGQK_.push(pzzDo)
            //   repprt.F_PGQK_.push(jdjcsDo)
            //   repprt.F_PGQK_.push(dfsDo)
            //   repprt.F_PGQK_.push(xxx)

          })
          repprt.F_FJ_=""
          repprt.F_LDYJ_=""
          repprt.F_PGQK_=""
          repprt.F_ZGBG_=filelist
          lanjian_supervision_report.data.push(repprt)
        }
      })
      console.log("fafaf---",lanjian_supervision_plan)
      fs.writeFileSync(zippath+'/data/lanjian_supervision_report',ecodeFile(JSON.stringify(lanjian_supervision_report)))

      unitSelected[0].module="整改报告"
      fs.writeFileSync(zippath+"/Readme.json",ecodeFile(JSON.stringify(unitSelected[0])))
      module_name=module_name+"_"+"整改报告_"+getNowMilliSecond()


      compressing.zip.compressDir(zippath, zippath+ '.zip').then(() => {
        let fileb = fs.createReadStream(zippath+ '.zip')
        streamToBuffer(fileb).then(res => {
          let data = res
          let contents = new Uint8Array(data.length)
          for (let i = 0; i < data.length; i++) {
            contents[i] = data[i]
          }
          encrypt(new Blob([contents]),password,resolve,'导出',module_name,"整改报告")
        })
      })

    }
    /*---------------------------------------整改公开------------------------------------------------------*/
    if(type==5||type==0){
      const filelist_tb=[]
      const filelist_gk=[]
      const filelist_nogk=[]
      const report_dbson=fs.readFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson","utf-8")
      if(report_dbson==""){
        resolve(0)
        return;
      }
      const jl_dbson=getJLSelected()
      if(jl_dbson.length==0){
        resolve(0)
        return;
      }
      let lsarr = JSON.parse(report_dbson).filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        resolve(0)
        return;
      }
      if(lsarr[0].planTotal.f_status=="2"||lsarr[0].planTotal.f_status=="5"){
        resolve(2)
        return;
      }
      lsarr.forEach(item=>{
        //console.log('dd--',JSON.parse(jl_dbson)[0].unitId)
        const gks={}
        gks.F_XCZDX_REF_ID_=jl_dbson[0].unitId
        gks.ID_=item.id+""
        gks.CREATE_DEP_ID_="1"
        gks.TENANT_ID_=tenantId+""
        gks.F_BJR_=item.planTotal.editor
        gks.F_VERSION_= item.planTotal.version+""
        gks.F_STATUS_=item.planTotal.f_status==undefined?"1":(item.planTotal.f_status=="3"||item.planTotal.f_status=="6")?"4":"1"
        gks.CREATE_BY_="1"
        gks.CREATE_TIME_=getcurDate()
        // gks.F_LDYJ_=""
        // gks.F_PGQK_=""
        // gks.F_FJ_=""
        if(item.planTotal.fileList_tb.length>0){
          item.planTotal.fileList_tb.forEach(its=>{
            const fid=getNowMilliSecond()
            const sysfile={}
            const file={}
            file.uid=fid+""
            file.name=its.name
            file.fileId=fid+""
            file.fileName=its.name
            file.size=its.filesize
            let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
            file.newFname=`${fid}.${fileExt}`
            file.path="operation/"+file.newFname
            filelist_tb.push(file)
            //复制文件
            fs.copyFileSync(its.url, zippath+'/file/'+file.newFname)
            //组装sys_file 文件shuju
            // sysfile.TOTAL_BYTES_=its.filesize
            // sysfile.FILE_ID_=fid
            // sysfile.NEW_FNAME_=file.newFname
            // sysfile.UPDATE_TIME_=its.updateTime
            // sysfile.CREATE_TIME_=its.updateTime
            // sysfile.EXT_=fileExt
            // sysfile.DEL_STATUS_="undeleted"
            // sysfile.PATH_=file.path
            // sysfile.FILE_NAME_=its.name
            // sysfile.CREATE_BY_="1"
            // sysfile.TENANT_ID_="1"
            // sysfile.FILE_SYSTEM_="file"
            // sysfile.ENCRYPT="0"
            // sys_files.data.push(sysfile)
          })
          gks.F_DNTBG_=filelist_tb
        }
        if(item.planTotal.fileList_gk.length>0){
          item.planTotal.fileList_gk.forEach(its=>{
            const fid=getNowMilliSecond()
            const sysfile={}
            const file={}
            file.uid=fid+""
            file.name=its.name
            file.fileId=fid+""
            file.fileName=its.name
            file.size=its.filesize
            let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
            file.newFname=`${fid}.${fileExt}`
            file.path="operation/"+file.newFname
            filelist_gk.push(file)
            //复制文件
            fs.copyFileSync(its.url, zippath+'/file/'+file.newFname)
            //组装sys_file 文件shuju
            // sysfile.TOTAL_BYTES_=its.filesize
            // sysfile.FILE_ID_=fid
            // sysfile.NEW_FNAME_=file.newFname
            // sysfile.UPDATE_TIME_=its.updateTime
            // sysfile.CREATE_TIME_=its.updateTime
            // sysfile.EXT_=fileExt
            // sysfile.DEL_STATUS_="undeleted"
            // sysfile.PATH_=file.path
            // sysfile.FILE_NAME_=its.name
            // sysfile.CREATE_BY_="1"
            // sysfile.TENANT_ID_="1"
            // sysfile.FILE_SYSTEM_="file"
            // sysfile.ENCRYPT="0"
            // sys_files.data.push(sysfile)
          })
          gks.F_SHGKG_=filelist_gk
        }
        if(item.planTotal.fileList_gk.length>0){
          item.planTotal.fileList_nogk.forEach(its=>{
            const fid=getNowMilliSecond()
            const sysfile={}
            const file={}
            file.uid=fid+""
            file.name=its.name
            file.fileId=fid+""
            file.fileName=its.name
            file.size=its.filesize
            let fileExt = its.name.slice(its.name.lastIndexOf('.') + 1)
            file.newFname=`${fid}.${fileExt}`
            file.path="operation/"+file.newFname
            filelist_nogk.push(file)
            //复制文件
            fs.copyFileSync(its.url, zippath+'/file/'+file.newFname)
            //组装sys_file 文件shuju
            // sysfile.TOTAL_BYTES_=its.filesize
            // sysfile.FILE_ID_=fid
            // sysfile.NEW_FNAME_=file.newFname
            // sysfile.UPDATE_TIME_=its.updateTime
            // sysfile.CREATE_TIME_=its.updateTime
            // sysfile.EXT_=fileExt
            // sysfile.DEL_STATUS_="undeleted"
            // sysfile.PATH_=file.path
            // sysfile.FILE_NAME_=its.name
            // sysfile.CREATE_BY_="1"
            // sysfile.TENANT_ID_="1"
            // sysfile.FILE_SYSTEM_="file"
            // sysfile.ENCRYPT="0"
            // sys_files.data.push(sysfile)
          })
          gks.F_SXSM_=filelist_nogk
        }
        lanjian_supervision_public.data.push(gks)
      })
      const gklists=JSON.parse(report_dbson)
      gklists.forEach(ib=>{
        lsarr.forEach(ibs=>{
          if(ib.id===ibs.id){
            if(ib.planTotal.f_status===undefined){
              ib.planTotal.f_status="1"
            }else {
              ib.planTotal.f_status=(ibs.planTotal.f_status==="3"||ibs.planTotal.f_status==="6")?"4":ibs.planTotal.f_status
            }
          }
        })
      })

      fs.writeFileSync(getAppLocalPath()+'dbson/RectificationZGGKAllinfos.dbson',JSON.stringify(gklists))

      console.log("fafaf---",lanjian_supervision_public)
      fs.writeFileSync(zippath+'/data/lanjian_supervision_public',ecodeFile(JSON.stringify(lanjian_supervision_public)))

      unitSelected[0].module="整改公开"
      fs.writeFileSync(zippath+"/Readme.json",ecodeFile(JSON.stringify(unitSelected[0])))
      module_name=module_name+"_"+"整改公开_"+getNowMilliSecond()

      compressing.zip.compressDir(zippath, zippath+ '.zip').then(() => {
        let fileb = fs.createReadStream(zippath+ '.zip')
        streamToBuffer(fileb).then(res => {
          let data = res
          let contents = new Uint8Array(data.length)
          for (let i = 0; i < data.length; i++) {
            contents[i] = data[i]
          }
          encrypt(new Blob([contents]),password,resolve,'导出',module_name,"整改公开")
        })
      })

    }
  })
}

function importAndOutRecord(module,path,fname,type){
  let node = {}
  node.id = getNowMilliSecond()
  node.type = type
  node.module = module
  node.date = getcurDate()
  node.file = fname
  node.size = getFileSize(fs.statSync(path).size)
  node.path = path
  node.fname = fname
  //绑定结论组
  node.jlid=getJLID()
  setNodeOfUpAndExport(node)
}

export const encodeGbk=(str)=>{
  return iconv.encode(Buffer.from(str), 'gbk');
}

export const  getJLSelected=()=>{
  const jlselected=fs.readFileSync(getAppLocalPath()+'dbson/lanjianJLSelected.dbson', 'utf-8')
  return jlselected==""?[]:JSON.parse(jlselected)
}
export const  getUnitsinfos=()=>{
  const units=fs.readFileSync(getAppLocalPath()+'dbson/lanjianUnitInfos.dbson', 'utf-8')
  return units==""?[]:JSON.parse(units)
}

export const  setJLSelected=(id)=>{
  const units_dbson_ayyay=[];
  const units_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+'dbson/lanjianUnitInfos.dbson',"utf-8"))
  units_dbson.forEach(item=>{
    if((item.sessionId+item.roundId)===id) {
      units_dbson_ayyay.push(item)
      fs.writeFileSync(getAppLocalPath()+'dbson/lanjianJLSelected.dbson', JSON.stringify(units_dbson_ayyay))
      console.log("116--",units_dbson_ayyay)
    }
  })
}
function encrypt(file, password,relsove,type,folder,module) {
  const Minizip = require('minizip-asm.js')
  const lid=getNowMilliSecond()
  const fr = new FileReader()
  fr.onload = function(event) {
    try {
      const zip = new Minizip(new Uint8Array(event.target.result));
      const newZip = new Minizip();
      zip.list({encoding: "buffer"}).forEach(function(o) {
        newZip.append(o.filepath, zip.extract(o.filepath), {password: password});
        console.log("ps---",o.filepath)
      });
      fs.writeFileSync(getSystemDir()+"/"+folder+".zip", new Buffer(newZip.zip()));
      importAndOutRecord(module,getSystemDir()+"/"+folder+".zip",folder+".zip",type)
      relsove(1)
    } catch (e) {
      relsove(0)
      console.log("ps-2--",e)
    }
  }
  fr.readAsArrayBuffer(file);
}
export const getZGFAHistory=()=>{
  console.log("fas1---","JSON.parse(history.data)")
  return new Promise( (resolve, reject) => {
    try {
      const history=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/ZGFAHistory.dbson","utf-8"))
      resolve((history.data).reverse())
    }catch (e) {
      console.log("fres--",e)
      resolve([])
    }

  })
}

export const getMeetHistory=(record)=>{
  console.log("fas1---","JSON.parse(history.data)")
  return new Promise( (resolve, reject) => {
    try {
      const history=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/MEETINGHistory.dbson","utf-8")).data
      const relist=history.filter(is=>{
        return is.F_REL_ID_===record.id
      })
      resolve(relist.reverse())
    }catch (e) {
      console.log("fres--",e)
      resolve([])
    }
  })
}

export const getZGTZHistory=()=>{
  console.log("fas1---","JSON.parse(history.data)")
  return new Promise( (resolve, reject) => {
    try {
      const history=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/LeaderHistory.dbson","utf-8"))
      resolve((history.data).reverse())
    }catch (e) {
      console.log("fres--",e)
      resolve([])
    }

  })
}

export const getZGGKHistory=()=>{
  console.log("fas1---","JSON.parse(history.data)")
  return new Promise( (resolve, reject) => {
    try {
      const history=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/ZGGKHistory.dbson","utf-8"))
      resolve((history.data).reverse())
    }catch (e) {
      console.log("fres--",e)
      resolve([])
    }

  })
}


export const getZGBGHistory=()=>{
  console.log("fas1---","JSON.parse(history.data)")
  return new Promise( (resolve, reject) => {
    try {
      const history=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/ReportHistory.dbson","utf-8"))
      resolve((history.data).reverse())
    }catch (e) {
      console.log("fres--",e)
      resolve([])
    }

  })
}
export const getTemplete=()=> {

  return new Promise((resolve, reject) => {
    const tmp = JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Templete.dbson", "utf-8") == "" ? "[]" : fs.readFileSync(getAppLocalPath()+"dbson/Templete.dbson", "utf-8"))
    const tmp2=tmp.filter(function (value) {
      return  value.jlid == getJLID()
    })
    console.log("fas1---", tmp2)
    resolve(tmp2)
  })
}
export const getWarings=()=>{
  console.log("fas1---","tmp")
  return new Promise( (resolve, reject) => {
    const tmp=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Warings.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Warings.dbson","utf-8"))
    const tmp2=tmp.filter(function (value) {
      return   value.jlid ===getJLID()
    })
    resolve(tmp)
  })

}

export const getFKSJ=()=>{
  console.log("fas1---","tmp")
  return new Promise( (resolve, reject) => {
    const tmp=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/Warings.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/Warings.dbson","utf-8"))
    const tmp2=tmp.filter(function (value) {
      return   value.jlid ===getJLID()
    })
    resolve(tmp2)
  })
}
//更新整改方案
export const updatFeedbackPlan=(data)=>{
  console.log("ls-1-", data)
  var util = require('util');
  return new Promise( (resolve, reject) => {
    const backpaln = fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson", "utf-8")
    const list = JSON.parse(backpaln == "" ? "[]" : backpaln)
    for (var i = 0; i < list.length; i++) {
      data.forEach(item => {
        console.log("ls--", item)
        if (item['编号'] === list[i].id) {
          list[i].zgfa_measure = item['整改措施']===undefined?"":item['整改措施']
          list[i].zgfa_time = item['整改时限']===undefined?"":item['整改时限']
          list[i].zgfa_zgtype = getZGTypeResv(item['整改类型']===undefined?"":item['整改类型']+"")
          list[i].zgfa_department = item['责任部门']===undefined?"":item['责任部门']
          list[i].zgfa_leader = item['责任领导']===undefined?"":item['责任领导']
          list[i].zgfa_persion = item['责任人']===undefined?"":item['责任人']
          list[i].zgfa_endtime = (item['完成时限']===undefined||item['完成时限']==="")?null:moment(new Date(item['完成时限']+"")).format("YYYY-MM-DD")
        }
      })
    }
    fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson", JSON.stringify(list))
    resolve(1)
  })
}
//整改台账
export const updatFeedbackTZ=(data)=>{
  return new Promise( (resolve, reject) => {
    const backpaln = fs.readFileSync(getAppLocalPath()+"dbson/Feedback.dbson", "utf-8")
    const list = JSON.parse(backpaln == "" ? "[]" : backpaln)
    for (var i = 0; i < list.length; i++) {
      data.forEach(item => {
        console.log("ls--", item['编号'])
        if (item['编号'] === list[i].id) {
          list[i].zgtz_whss=item['挽回损失(万)']===undefined?"":item['挽回损失(万)']
            list[i].zgtz_zrr=item['追责问责(人)']===undefined?"":item['追责问责(人)']
            list[i].zgtz_wszd=item['完善制度(项)']===undefined?"":item['完善制度(项)']
            list[i].zgtz_complete_info=getComTypeResv(item['整改完成情况']===undefined?"":item['整改完成情况']+"")
            list[i].zgtz_marks=item['备注']===undefined?"":item['备注']
        }
      })
    }
    fs.writeFileSync(getAppLocalPath()+"dbson/Feedback.dbson", JSON.stringify(list))
    resolve(1)
  })
}

export const setPlanExtime=()=>{
  const time_json= fs.readFileSync(getAppLocalPath()+"dbson/ZGDate.dbson","utf-8")
  const time_list=JSON.parse(time_json===""?"[]":time_json)
  const tm_list=time_list.length>0?time_list.filter(function (value) {
    return   value.jlid ===getJLID()
  }):[]
  const tm_listno=time_list.length>0?time_list.filter(function (value) {
    return   value.jlid !=getJLID()
  }):[]
  let t_json= {}

  if(tm_list.length>0){
    if(tm_list[0].f_time==undefined){
      tm_list[0].f_time=moment(new Date()).format("YYYY-MM-DD")
      tm_listno.push(tm_list[0])
      fs.writeFileSync(getAppLocalPath()+"dbson/ZGDate.dbson",JSON.stringify(tm_listno))
    }
  }
  else{
    t_json.jlid=getJLID()
    t_json.f_time=moment(new Date()).format("YYYY-MM-DD")
    time_list.push(t_json)
    fs.writeFileSync(getAppLocalPath()+"dbson/ZGDate.dbson",JSON.stringify(time_list))
  }
}
export const setReportExtime=()=>{
  const time_json= fs.readFileSync(getAppLocalPath()+"dbson/ZGDate.dbson","utf-8")
  const time_list=JSON.parse(time_json==""?"[]":time_json)
  const tm_list=time_list.length>0?time_list.filter(function (value) {
    return   value.jlid ===getJLID()
  }):[]
  const tm_listno=time_list.length>0?time_list.filter(function (value) {
    return   value.jlid !=getJLID()
  }):[]
  let t_json={}

  if(tm_list.length>0){
    if(tm_list[0].r_time==undefined){
      tm_list[0].r_time=moment(new Date()).format("YYYY-MM-DD")
      tm_listno.push(tm_list[0])
      fs.writeFileSync(getAppLocalPath()+"dbson/ZGDate.dbson",JSON.stringify(tm_listno))
    }
  }
  else{
    t_json.jlid=getJLID()
    t_json.r_time=moment(new Date()).format("YYYY-MM-DD")
    time_list.push(t_json)
    fs.writeFileSync(getAppLocalPath()+"dbson/ZGDate.dbson",JSON.stringify(time_list))
  }
}

export const getAllExtime=()=>{
  const time_json = fs.readFileSync(getAppLocalPath()+"dbson/ZGDate.dbson", "utf-8")
  const time_list = JSON.parse(time_json === "" ? "[]" : time_json)
  const tm_list = time_list.length > 0 ? time_list.filter(function (value) {
    return value.jlid === getJLID()
  }) : []
  console.log("rm--",tm_list)
  if(tm_list===undefined||tm_list.length===0){
    return JSON.parse("{}")
  }
  return tm_list[0]

}
